package com.example.littleken.diglett;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.ref.WeakReference;
import java.util.Random;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnTouchListener {
    public static final int MSG_SIGN = 123;
    private TextView mResultTextView;
    private ImageView mTianYiImageView;
    private Button mStartButton;

    public int[][] mPosition = new int[][]{//模拟地鼠位置
            {342,180},{432,880},{521,256},{429,780},
            {456,976},{145,665},{123,678},{564,567}
    };
    private int mTotalCount;
    private int mSuccessCount;

    public static  final int MAX_COUNT = 10;

    private DiglettHandler mHandler = new DiglettHandler(this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setTitle("打地鼠");
    }

    private void initView() {
        mResultTextView = (TextView) findViewById(R.id.textview);
        mTianYiImageView = (ImageView) findViewById(R.id.imageView);
        mStartButton = (Button) findViewById(R.id.btn_start);
        mStartButton.setOnClickListener(this);
        mTianYiImageView.setOnTouchListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_start:
                start();
                break;
        }
    }

    private void start() {
        mResultTextView.setText("开始啦");
        mStartButton.setText("游戏中……");
        mStartButton.setEnabled(false);
        next(0);
    }

    private void next(int delayTime){
        int position = new Random().nextInt(mPosition.length);
        Message message = Message.obtain();
        message.what = MSG_SIGN;
        message.arg1 = position;
        mHandler.sendMessageDelayed(message,delayTime);
        mTotalCount ++;
    }
    private void clear(){
        mTotalCount = 0;
        mSuccessCount = 0;
        mTianYiImageView.setVisibility(View.GONE);
        mStartButton.setText("开始");
        mStartButton.setEnabled(true);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        v.setVisibility(View.GONE);
        mSuccessCount ++;
        mResultTextView.setText("命中："+mSuccessCount + "共：" + MAX_COUNT);
        return false;
    }

    public static class DiglettHandler extends Handler{
        private final WeakReference<MainActivity> mWeakReference;
        private static final int RANDOM_NUM = 500;

        public DiglettHandler(MainActivity activity) {
            this.mWeakReference = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            MainActivity activity = mWeakReference.get();
            switch (msg.what){
                case  MSG_SIGN :
                    if(activity.mTotalCount > MAX_COUNT){
                        activity.clear();
                        Toast.makeText(activity,"游戏结束",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    int postion = msg.arg1;
                    activity.mTianYiImageView.setX(activity.mPosition[postion][0]);
                    activity.mTianYiImageView.setY(activity.mPosition[postion][1]);
                    activity.mTianYiImageView.setVisibility(View.VISIBLE);

                    int randomTime = new Random().nextInt(RANDOM_NUM) + RANDOM_NUM;
                    activity.next(randomTime);
                    break;
            }
        }
    }
}
